﻿using System.Collections.Generic;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace WebApplication1.classes.Core.JSTree
{
    public class JsTree {
        public List<JsTreeNode> data;

        public JsTree() {
            data = new List<JsTreeNode>();
        }
    }

    public static class JsTreeExtensionMethods {

        public static string jsonString( this object aObject) {

            var javaScriptSerializer = new JavaScriptSerializer();
            return javaScriptSerializer.Serialize(aObject);
        }

        /*  addNode
         * creates node in tree
         */
        public static JsTreeNode addNode(this JsTree jsTree, string title) {
            var node = new JsTreeNode(title);
            jsTree.data.Add( node );
            return node;
        }

        /*  addNodes
         * creates nodes in tree with titles
         */
        public static List<JsTreeNode> addNodes(this JsTree jsTree, params string[] titles)
        {
            var nodes = new List<JsTreeNode>();
            foreach(var title in titles) {
                nodes.Add( jsTree.addNode( title ) );
            }
            return nodes;
        }

        /*  addNode
         * creates node in treeNodes with title
         */
        public static JsTreeNode addNode(this JsTreeNode jsTreeNode, string title)
        {
            var node = new JsTreeNode( title );
            jsTreeNode.children.Add( node );
            return node;
        }

        /*  addNodes
         * creates nodes in nodes with titles
         */
        public static List<JsTreeNode> addNodes(this JsTreeNode jsTreeNode, params string[] titles)
        {
            var nodes = new List<JsTreeNode>();
            foreach (var title in titles) {
                var node = new JsTreeNode( title );
                nodes.Add( node );
            }

            return nodes;
        }

    }
}